package GameData;

import java.util.*;

public class Games {

	static
	{
		games = new ArrayList<Game>();
		
		teamNames = new HashMap<String, String>();
		numToMonthConverter = new HashMap<Integer, String>();
		monthToNumConverter = new HashMap<String, Integer>();
		daySpellingToNumConverter = new HashMap<String, Integer>();
		initTeamNames();
		initMonthConverter();
		initDaySpellingConverter();
	}
	
	private static void initTeamNames()
	{
		teamNames.put("MIL", "Milwaukee Brewers");
		teamNames.put("milwaukee brewers", "Milwaukee Brewers");
		teamNames.put("milwaukee", "Milwaukee Brewers");
		teamNames.put("brewers", "Milwaukee Brewers");
		
		teamNames.put("CIN", "Cincinatti Reds");
		teamNames.put("cincinatti reds", "Cincinatti Reds");
		teamNames.put("cincinatti", "Cincinatti Reds");
		teamNames.put("reds", "Cincinatti Reds");
		
		teamNames.put("ANA", "Los Angeles Angels of Anaheim");
		teamNames.put("los angeles angels of anaheim", "Los Angeles Angels of Anaheim");
		teamNames.put("angels", "Los Angeles Angels of Anaheim");
		teamNames.put("los angeles angels", "Los Angeles Angels of Anaheim");
		
		teamNames.put("KCA", "Kansas City Royals");
		teamNames.put("kansas city royals", "Kansas City Royals");
		teamNames.put("kansas", "Kansas City Royals");
		teamNames.put("kansas city", "Kansas City Royals");
		teamNames.put("royals", "Kansas City Royals");
		
		teamNames.put("SFN", "San Francisco Giants");
		teamNames.put("san francisco giants", "San Francisco Giants");
		teamNames.put("san francisco", "San Francisco Giants");
		teamNames.put("giants", "San Francisco Giants");
		
		teamNames.put("LAN", "Los Angeles Dodgers");
		teamNames.put("los angeles dodgers", "Los Angeles Dodgers");
		teamNames.put("dodgers", "Los Angeles Dodgers");
		
		teamNames.put("DET", "Detroit Tigers");
		teamNames.put("detroit tigers", "Detroit Tigers");
		teamNames.put("detroit", "Detroit Tigers");
		teamNames.put("tigers", "Detroit Tigers");
		
		teamNames.put("NYA", "New York Yankees");
		teamNames.put("new york yankees", "New York Yankees");
		teamNames.put("yankees", "New York Yankees");
		
		teamNames.put("SDN", "San Diego Padres");
		teamNames.put("san diego padres", "San Diego Padres");
		teamNames.put("san diego", "San Diego Padres");
		teamNames.put("padres", "San Diego Padres");
		
		teamNames.put("SLN", "Saint Louis Cardinals");
		teamNames.put("saint louis cardinals", "Saint Louis Cardinals");
		teamNames.put("saint louis", "Saint Louis Cardinals");
		teamNames.put("cardinals", "Saint Louis Cardinals");
		
		teamNames.put("ATL", "Atlanta Braves");
		teamNames.put("atlanta braves", "Atlanta Braves");
		teamNames.put("atlanta", "Atlanta Braves");
		teamNames.put("braves", "Atlanta Braves");
		
		teamNames.put("WAS", "Washington Nationals");
		teamNames.put("washington nationals", "Washington Nationals");
		teamNames.put("washington", "Washington Nationals");
		teamNames.put("nationals", "Washington Nationals");
		
		teamNames.put("PIT", "Pittsburgh Pirates");
		teamNames.put("pittsburgh pirates", "Pittsburgh Pirates");
		teamNames.put("pittsburgh", "Pittsburgh Pirates");
		teamNames.put("pirates", "Pittsburgh Pirates");
		
		teamNames.put("CHN", "Chicago Cubs");
		teamNames.put("chicago cubs", "Chicago Cubs");
		teamNames.put("cubs", "Chicago Cubs");
		
		teamNames.put("CHA", "Chicago White Sox");
		teamNames.put("chicago white sox", "Chicago White Sox");
		teamNames.put("chicago white socks", "Chicago White Sox");
		teamNames.put("white socks", "Chicago White Sox");
		teamNames.put("white sox", "Chicago White Sox");
		
		teamNames.put("CLE", "Cleveland Indians");
		teamNames.put("cleveland indians", "Cleveland Indians");
		teamNames.put("cleveland", "Cleveland Indians");
		teamNames.put("indians", "Cleveland Indians");
		
		teamNames.put("ARI", "Arizona Diamondbacks");
		teamNames.put("arizona diamondbacks", "Arizona Diamondbacks");
		teamNames.put("arizona diamond backs", "Arizona Diamondbacks");
		teamNames.put("arizona", "Arizona Diamondbacks");
		teamNames.put("diamondbacks", "Arizona Diamondbacks");
		
		teamNames.put("COL", "Colorado Rockies");
		teamNames.put("colorado rockies", "Colorado Rockies");
		teamNames.put("colorado", "Colorado Rockies");
		teamNames.put("rockies", "Colorado Rockies");
		
		teamNames.put("NYN", "New York Mets");
		teamNames.put("new york mets", "New York Mets");
		teamNames.put("mets", "New York Mets");
		
		teamNames.put("FLO", "Florida Marlins");
		teamNames.put("florida marlins", "Florida Marlins");
		teamNames.put("florida", "Florida Marlins");
		teamNames.put("marlins", "Florida Marlins");
		
		teamNames.put("SEA", "Seattle Mariners");
		teamNames.put("seattle mariners", "Seattle Mariners");
		teamNames.put("seattle", "Seattle Mariners");
		teamNames.put("seahawks", "Seattle Mariners");
		
		teamNames.put("OAK", "Oakland As");
		teamNames.put("oakland athletics", "Oakland As");
		teamNames.put("oakland as", "Oakland As");
		teamNames.put("as", "Oakland As");
		teamNames.put("athletics", "Oakland As");
		
		teamNames.put("HOU", "Houston Astros");
		teamNames.put("houston astros", "Houston Astros");
		teamNames.put("houston", "Houston Astros");
		teamNames.put("astros", "Houston Astros");
		
		teamNames.put("PHI", "Philadelphia Phillies");
		teamNames.put("philadelphia", "Philadelphia Phillies");
		teamNames.put("philadelphia phillies", "Philadelphia Phillies");
		teamNames.put("philadelphia philies", "Philadelphia Phillies");
		teamNames.put("phillies", "Philadelphia Phillies");
		teamNames.put("philies", "Philadelphia Phillies");
		
		teamNames.put("BAL", "Baltimore Orioles");
		teamNames.put("baltimore", "Baltimore Orioles");
		teamNames.put("baltimore orioles", "Baltimore Orioles");
		teamNames.put("orioles", "Baltimore Orioles");
		
		teamNames.put("TBA", "Tampa Bay Rays");
		teamNames.put("tampa bay", "Tampa Bay Rays");
		teamNames.put("tampa bay rays", "Tampa Bay Rays");
		teamNames.put("rays", "Tampa Bay Rays");
		
		teamNames.put("BOS", "Boston Red Sox");
		teamNames.put("boston", "Boston Red Sox");
		teamNames.put("boston red socks", "Boston Red Sox");
		teamNames.put("boston red sox", "Boston Red Sox");
		teamNames.put("red socks", "Boston Red Sox");
		teamNames.put("red sox", "Boston Red Sox");
		
		teamNames.put("TEX", "Texas Rangers");
		teamNames.put("texas", "Texas Rangers");
		teamNames.put("texas rangers", "Texas Rangers");
		teamNames.put("rangers", "Texas Rangers");
		
		teamNames.put("MIN", "Minnesota Twins");
		teamNames.put("minnesota", "Minnesota Twins");
		teamNames.put("minnesota twins", "Minnesota Twins");
		teamNames.put("twins", "Minnesota Twins");
		
		teamNames.put("TOR", "Toronto Blue Jays");
		teamNames.put("toronto", "Toronto Blue Jays");
		teamNames.put("toronto blue jays", "Toronto Blue Jays");
		teamNames.put("blue jays", "Toronto Blue Jays");
	}
	private static void initMonthConverter()
	{
		numToMonthConverter.put(1, "january");
		numToMonthConverter.put(2, "february");
		numToMonthConverter.put(3, "march");
		numToMonthConverter.put(4, "april");
		numToMonthConverter.put(5, "may");
		numToMonthConverter.put(6, "june");
		numToMonthConverter.put(7, "july");
		numToMonthConverter.put(8, "august");
		numToMonthConverter.put(9, "september");
		numToMonthConverter.put(10, "october");
		numToMonthConverter.put(11, "november");
		numToMonthConverter.put(12, "december");
		
		monthToNumConverter.put("january", 1);
		monthToNumConverter.put("february", 2);
		monthToNumConverter.put("march", 3);
		monthToNumConverter.put("april", 4);
		monthToNumConverter.put("may", 5);
		monthToNumConverter.put("june", 6);
		monthToNumConverter.put("july", 7);
		monthToNumConverter.put("august", 8);
		monthToNumConverter.put("september", 9);
		monthToNumConverter.put("october", 10);
		monthToNumConverter.put("november", 11);
		monthToNumConverter.put("december", 12);
	}
	
	private static void initDaySpellingConverter()
	{
		daySpellingToNumConverter.put("first", 1);
		daySpellingToNumConverter.put("second", 2);
		daySpellingToNumConverter.put("third", 3);
		daySpellingToNumConverter.put("fourth", 4);
		daySpellingToNumConverter.put("fifth", 5);
		daySpellingToNumConverter.put("sixth", 6);
		daySpellingToNumConverter.put("seventh", 7);
		daySpellingToNumConverter.put("eighth", 8);
		daySpellingToNumConverter.put("ninth", 9);
		daySpellingToNumConverter.put("tenth", 10);
		daySpellingToNumConverter.put("eleventh", 11);
		daySpellingToNumConverter.put("twelfth", 12);
		daySpellingToNumConverter.put("thirteenth", 13);
		daySpellingToNumConverter.put("fourteenth", 14);
		daySpellingToNumConverter.put("fifteenth", 15);
		daySpellingToNumConverter.put("sixteenth", 16);
		daySpellingToNumConverter.put("seventeenth", 17);
		daySpellingToNumConverter.put("eighteenth", 18);
		daySpellingToNumConverter.put("nineteenth", 19);
		daySpellingToNumConverter.put("twentieth", 20);
		daySpellingToNumConverter.put("twenty first", 21);
		daySpellingToNumConverter.put("twenty second", 22);
		daySpellingToNumConverter.put("twenty third", 23);
		daySpellingToNumConverter.put("twenty fourth", 24);
		daySpellingToNumConverter.put("twenty fifth", 25);
		daySpellingToNumConverter.put("twenty sixth", 26);
		daySpellingToNumConverter.put("twenty seventh", 27);
		daySpellingToNumConverter.put("twenty eighth", 28);
		daySpellingToNumConverter.put("twenty ninth", 29);
		daySpellingToNumConverter.put("thirtieth", 30);
		daySpellingToNumConverter.put("thirty first", 31);
	}
	
	public static ArrayList<Game> games;
	public static HashMap<String, String> teamNames;
	public static HashMap<Integer, String> numToMonthConverter;
	public static HashMap<String, Integer> monthToNumConverter;
	public static HashMap<String, Integer> daySpellingToNumConverter;
	
	/**
	 * Searches for a game by the home team and game date (as a string)
	 * @param homeTeam
	 * @param gameDate date of game, i.e. "April 27"
	 * @return
	 */
	public static Game searchByHomeTeam(String homeTeam, String gameDate)
	{
		String[] dateSplit = gameDate.trim().split(" ");
		int theMonth = monthToNumConverter.get(dateSplit[0].toLowerCase());
		//String dayNumber = (dateSplit[1] + (dateSplit.length>2?dateSplit[2]:"")).trim();
		//int theDay = daySpellingToNumConverter.get(dayNumber);
		int theDay = Integer.parseInt(dateSplit[1]);
		return searchByHomeTeam(homeTeam, theMonth, theDay);
	}
	
	/**
	 * Gets the next available game, given a home team
	 * @param homeTeam
	 * @return
	 */
	public static Game searchNextHomeTeam(String homeTeam)
	{
		int todayMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
		int todayDay = Calendar.getInstance().get(Calendar.DATE);
		return searchByHomeTeam(homeTeam, todayMonth, todayDay);
	}
	
	/**
	 * Searches for a game by the home team and game date (as a string)
	 * @param homeTeam
	 * @param awayTeam
	 * @param gameDate date of game, i.e. "August 1"
	 * @return
	 */
	public static Game searchByHomeAwayTeam(String homeTeam, String awayTeam, String gameDate)
	{
		String[] dateSplit = gameDate.trim().split(" ");
		int theMonth = monthToNumConverter.get(dateSplit[0].toLowerCase());
		//String dayNumber = (dateSplit[1] + (dateSplit.length>2?(" "+dateSplit[2]):"")).trim();
		//int theDay = daySpellingToNumConverter.get(dayNumber);
		int theDay = Integer.parseInt(dateSplit[1]);
		return searchByHomeAwayTeam(homeTeam, awayTeam, theMonth, theDay);
	}
	
	/**
	 * Gets the next available game given a home and away team
	 * @param homeTeam
	 * @param awayTeam
	 * @return
	 */
	public static Game searchNextHomeAwayTeam(String homeTeam, String awayTeam)
	{
		System.out.println("home team: " + homeTeam);
		System.out.println("away team: " + awayTeam + "\n");		
		int todayMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
		int todayDay = Calendar.getInstance().get(Calendar.DATE);
		return searchByHomeAwayTeam(homeTeam, awayTeam, todayMonth, todayDay);
	}
	
	/**
	 * Searches for a game by the home team and game date (as numbers)
	 * @param homeTeam
	 * @param dateMonth
	 * @param dateDay
	 * @return
	 */
	//gets the next game on or after the specified date
	public static Game searchByHomeTeam(String homeTeam, int dateMonth, int dateDay)
	{
		String theHomeTeam = Games.teamNames.get(homeTeam.toLowerCase().trim()).toLowerCase();
		for(Game g : games)
		{
			if(g.dateMonth < dateMonth)
				continue;
			if(g.dateDay < dateDay)
				continue;
			if(g.homeTeam.toLowerCase().equals(theHomeTeam))
				return g;
		}
		return null;
	}
	
	/**
	 * Searches for a game by the home team, away team, and game date (as numbers)
	 * @param homeTeam
	 * @param awayTeam
	 * @param dateMonth
	 * @param dateDay
	 * @return
	 */
	public static Game searchByHomeAwayTeam(String homeTeam, String awayTeam, int dateMonth, int dateDay)
	{
		System.out.println("home team: " + homeTeam);
		System.out.println("away team: " + awayTeam);
		System.out.println("dateMonth: " + dateMonth);
		System.out.println("dateDay: " + dateDay);
		String theHomeTeam = Games.teamNames.get(homeTeam.toLowerCase().trim()).toLowerCase();
		String theAwayTeam = Games.teamNames.get(awayTeam.toLowerCase().trim()).toLowerCase();
		for(Game g : games)
		{
			if(g.dateMonth < dateMonth)
				continue;
			if(g.dateDay < dateDay)
				continue;
			if(g.homeTeam.toLowerCase().equals(theHomeTeam) && 
					g.awayTeam.toLowerCase().equals(theAwayTeam))
			{
				System.out.println("Found a game!");				
				return g;
			}
		}
		System.out.println("couldn't find a game\n\n");		
		return null;
	}
}
